package com.xdcplus.web.service.bd.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xdcplus.mom.enums.ResponseEnum;
import com.xdcplus.mp.service.impl.BaseServiceImpl;
import com.xdcplus.web.common.pojo.dto.bd.BdMaterialFilterDTO;
import com.xdcplus.web.common.pojo.entity.bd.BdMaterial;
import com.xdcplus.web.common.pojo.query.bd.BdMaterialQuery;
import com.xdcplus.web.common.pojo.vo.bd.BdMaterialVO;
import com.xdcplus.web.mapper.bd.BdMaterialMapper;
import com.xdcplus.web.service.bd.BdMaterialService;
import com.xdcplus.web.service.bd.BdMaterialTypeService;
import com.xdcplus.web.service.bd.BdSupplierService;
import com.xdcplus.web.service.pm.PmWoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;

/**
 * 物料表(BdMaterial)表服务实现类
 *
 * @author Fish.Fei
 * @since 2023-02-02 15:45:47
 */
@Slf4j
@Service("bdMaterialService")
public class BdMaterialServiceImpl extends BaseServiceImpl<BdMaterialMapper, BdMaterial, BdMaterial, BdMaterialVO> implements BdMaterialService {

    @Autowired
    protected BdMaterialMapper bdMaterialMapper;

    @Autowired
    protected BdMaterialTypeService bdMaterialTypeService;

    @Autowired
    protected BdSupplierService bdSupplierService;

    @Autowired
    protected PmWoService pmWoService;

    @Override
    public Boolean saveOrUpdateBatch(List<BdMaterial> bdMaterialList) {

        bdMaterialList.forEach(bdMaterial -> {
            BdMaterial bdMaterialParam = new BdMaterial();
            bdMaterialParam.setId(bdMaterial.getId());
            if (ObjectUtil.isNotNull(bdMaterial.getId())) {
                bdMaterial.setId(bdMaterial.getId());
                bdMaterial.setUpdatedTime(new Timestamp(DateUtil.current()));
                LambdaUpdateWrapper<BdMaterial> lambdaUpdate = Wrappers.lambdaUpdate();
                lambdaUpdate.eq(BdMaterial::getId, bdMaterial.getId());
                update(bdMaterial, lambdaUpdate);
            } else {
                bdMaterial.setCreatedTime(new Timestamp(DateUtil.current()));
                save(bdMaterial);
            }
        });
        return true;
    }

    private List<BdMaterial> queryBdMaterialList(BdMaterialFilterDTO bdMaterialFilterDTO) {
        BdMaterialQuery bdMaterialQuery = BeanUtil.copyProperties(bdMaterialFilterDTO, BdMaterialQuery.class);

        return bdMaterialMapper.queryBdMaterial(bdMaterialQuery);
    }


    @Override
    public BdMaterialVO queryBdMaterialById(Long id) {

        Assert.notNull(id, ResponseEnum.THE_ID_CANNOT_BE_EMPTY.getMessage());

        return this.objectConversion(this.getById(id));
    }
    @Override
    public BdMaterialVO objectConversion(BdMaterial s) {
        BdMaterialVO bdMaterialVO = super.objectConversion(s);
        if(bdMaterialVO!=null) {
            if (bdMaterialVO.getTypeIdFk() != null) {
                bdMaterialVO.setBdMaterialTypeVO(bdMaterialTypeService.queryBdMaterialTypeById(bdMaterialVO.getTypeIdFk()));
            }
            Optional.ofNullable(bdMaterialVO.getSupplierIdFk()).ifPresent(a -> bdMaterialVO.setBdSupplierVO(bdSupplierService.queryBdSupplierById(a)));
        }
        return bdMaterialVO;
    }

    @Override
    public List<BdMaterialVO> objectConversion(List<BdMaterial> sList) {
        if (!CollectionUtil.isNotEmpty(sList)) {
            return null;
        } else {
            List<BdMaterialVO> tList = new ArrayList();
            Iterator var3 = sList.iterator();

            while(var3.hasNext()) {
                BdMaterial s = (BdMaterial)var3.next();
                BdMaterialVO t = this.objectConversion(s);
                Optional.ofNullable(t).ifPresent(tList::add);
            }

            return tList;
        }
    }

    @Override
    public List<BdMaterialVO> queryBdMaterialVOList(BdMaterialFilterDTO bdMaterialFilterDTO) {
        return this.objectConversion(queryBdMaterialList(bdMaterialFilterDTO));
    }

}
